package org.freeswitch.esl.client.test;

import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import lombok.extern.slf4j.Slf4j;
import org.freeswitch.esl.client.inbound.Client;
import org.freeswitch.esl.client.inbound.InboundConnectionFailure;
import org.freeswitch.esl.client.transport.CommandResponse;

/**
 * @author quehailin
 * @date 2020/5/29 21:10
 */
@Slf4j
public class FsClient extends Client {

    public static final FilterProvider filters;

    static {
        // 创建一个不序列化sex和weight的过滤器
        // 将上面的过滤器和ID为myFilter的注解进行关联
        filters = new SimpleFilterProvider().addFilter("myFilter", new SimpleBeanPropertyFilter() {
            @Override
            protected boolean include(PropertyWriter writer) {
                return filter(writer.getName());
            }

            private boolean filter(String fieldName) {
                // 排除以n开头的字段，以及int类型的字段
                // 只序列号包含uuid的字段
                return fieldName.contains("uuid");
            }
        });
    }

    /**
     * 记录最后一次心跳包时间<br>
     * 心跳包用来判断连接是否假死
     */
    public long lastHeartbeatTime = System.currentTimeMillis();


    public FsClient() {
        log.info("FsClient create");
    }

    @Override
    public CommandResponse close() {
        log.info("FsClient closed");
        return super.close();
    }

    @Override
    public void connect(String host, int port, String password, int timeoutSeconds) throws InboundConnectionFailure {
        log.info("开始连接:{}:{} {}", host, port, password);
        super.connect(host, port, password, timeoutSeconds);
    }

}
